﻿Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics
Imports Gadgeteer.Modules.Edu.Ghi


Namespace GadgeteerApp1
    Partial Public Class Program

        ' If you want to do something periodically, declare a GT.Timer by uncommenting the below line
        '   and then use the dropdown boxes at the top of this window to generate a Tick event handler.
        Dim WithEvents timer As GT.Timer = New GT.Timer(3000)  ' every 2 seconds (2000ms)


        Dim melody As Melody = New Melody()
        Dim amountLight As Double


        ' This is run when the mainboard is powered up or reset. 
        Public Sub ProgramStarted()
            ' Use Debug.Print to show messages in Visual Studio's "Immediate" window during debugging.
            Debug.Print("Program Started")
            'Create a melody that will run then an intruder is detected. 
            melody.Add(800, 300)
            melody.Add(300, 300)
            melody.Add(700, 300)
            melody.Add(200, 300)
            Timer.Start()

        End Sub


        Private Sub saveIntruderAlert(msg As String)


            Dim sdStorage As GT.StorageDevice = sdCard.GetStorageDevice()
            ' declare the intruderReport variable and encode the string “Alert!”
            ' into bytes to save it into your text file.
            Dim intruderReport As Byte() = System.Text.Encoding.UTF8.GetBytes(msg)
            Debug.Print(msg)
            ' set the boolean variable intruderAlert to True
            '  intruderAlert = True
            ' save the incident to the SD Card


            If sdCard.IsCardInserted() Then
                sdCard.MountSDCard()
                ' (which is needed for the WriteFile command
                Try


                    sdStorage.WriteFile("alerts.txt", intruderReport)
                Catch ex As Exception
                    ex.GetType()
                End Try
                sdCard.UnmountSDCard()
            Else
                Debug.Print("Intruder detected but cannot save this record to file!")
                Debug.Print("Insert the SD Card into the SD Module")
            End If
        End Sub



        Private Sub timer_Tick(timer As Gadgeteer.Timer) Handles Timer.Tick
            'Check the light sensor levels
            amountLight = lightSensor.GetIlluminance()
            Debug.Print("illuminence :" + amountLight.ToString()) 'Print the value to the debug window

            'If the alarm is too sensitive, change the alarm level (default 50)
            If amountLight > 50 Then
                Debug.Print("Intruder detected")
                tunes.Play(melody)
                saveIntruderAlert("Alert! Light level exceeded: " + amountLight.ToString())
            Else
                Debug.Print("No intruder detected, all OK")
                tunes.Stop() ' Turn off an alarm incase it was triggered.
            End If

        End Sub

        Private Sub button_ButtonPressed(sender As Gadgeteer.Modules.GHIElectronics.Button, state As Gadgeteer.Modules.GHIElectronics.Button.ButtonState) Handles button.ButtonPressed
            'Alarm triggered by the button
            tunes.Play(melody) 'sound alarm
            saveIntruderAlert("Alert! Button triggered")
        End Sub
    End Class
End Namespace